package com.mzba.easy.weibo.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.google.gson.Gson;
import com.mzba.easy.weibo.db.CommentEntry;
import com.mzba.easy.weibo.db.CommentGson;
import com.mzba.easy.weibo.db.EmotionEntry;
import com.mzba.easy.weibo.db.FavoriteEntryGson;
import com.mzba.easy.weibo.db.FavoritesGson;
import com.mzba.easy.weibo.db.StatusTimelineEntry;
import com.mzba.easy.weibo.db.StatusTimelineGson;
import com.mzba.easy.weibo.db.UserEntry;
import com.mzba.easy.weibo.db.UserGson;

/**
 * 
 * @author 06peng
 *
 */
public class WeiboManager {
	
	private Context context;
	private Gson gson;
	
	public WeiboManager(Context context) {
		this.context = context;
		gson = new Gson();
	}
	/**
	 * 获取用户以及用户关注的人发表的微博
	 * @param count
	 * @param page
	 * @return
	 */
	public StatusTimelineGson getFriendsTimeLine(int count, int page, String since_id) {
		StatusTimelineGson result = null;
		String request_url = AppContext.friend_timeline + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&page=" + page + "&since_id=" + since_id;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				result = gson.fromJson(response, StatusTimelineGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 根据微博id获取评论列表
	 * @param count
	 * @param page
	 * @param statusId
	 * @return
	 */
	public CommentGson getCommentsByStatusid(int count, int page, String statusId) {
		CommentGson result = null;
		String request_url = AppContext.get_comments_by_statusid + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&page=" + page + "&id=" + statusId;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				result = gson.fromJson(response, CommentGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 获取用户信息
	 * @param uid
	 * @return
	 */
	public UserEntry getUserByUserid(String uid) {
		UserEntry entry = null;
		String request_url = AppContext.get_user_by_userid + "?access_token=" + AppContext.getInstance().getAccess_token() + "&uid=" + uid;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				entry = gson.fromJson(response, UserEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entry;
	}
	
	/**
	 * 获取表情
	 * @return
	 */
	public List<EmotionEntry> getEmotions() {
		List<EmotionEntry> entries = new ArrayList<EmotionEntry>();
		String request_url = AppContext.get_emotions + "?access_token=" + AppContext.getInstance().getAccess_token();
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				JSONArray array = new JSONArray(response);
				for (int i = 0;i < array.length();i++) {
					JSONObject obj = array.getJSONObject(i);
					EmotionEntry entry = new EmotionEntry();
					entry.setCategory(obj.getString("category"));
					entry.setIcon(obj.getString("icon"));
					entry.setPhrase(obj.getString("phrase"));
					entry.setType(obj.getString("type"));
					entries.add(entry);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entries;
	}
	
	/**
	 * 评论一条微博
	 * @param comment
	 * @param id
	 * @param comment_ori 当评论转发微博时，是否评论给原微博，0：否、1：是，默认为0。
	 */
	public CommentEntry commentStatus(String comment, String id, int comment_ori) {
		CommentEntry entry = null;
		String request_url = AppContext.comment_status;
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
		paramsMap.put("comment", comment);
		paramsMap.put("id", id);
		paramsMap.put("comment_ori", String.valueOf(comment_ori));
		try {
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				entry = gson.fromJson(response, CommentEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entry;
	}
	
	/**
	 * 回复评论
	 * @param comment
	 * @param id
	 * @param cid
	 * @param comment_ori
	 * @return
	 */
	public CommentEntry replayCommemnt(String comment, String id, String cid, int comment_ori) {
		CommentEntry entry = null;
		String request_url = AppContext.comment_status;
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
		paramsMap.put("comment", comment);
		paramsMap.put("id", id);
		paramsMap.put("comment_ori", String.valueOf(comment_ori));
		paramsMap.put("cid", cid);
		paramsMap.put("without_mention", "1");
		try {
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				entry = gson.fromJson(response, CommentEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entry;
	}
	
	/**
	 * 获取评论列表
	 * @param count
	 * @param page
	 * @return
	 */
	public CommentGson getCommentsTimeline(String url, int count, int page, String since_id) {
		CommentGson result = null;
		String request_url = url + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&page=" + page + "&since_id=" + since_id;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				result = gson.fromJson(response, CommentGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取@我的微博
	 * @param count
	 * @param page
	 * @return
	 */
	public StatusTimelineGson getMentionsimeLine(int count, int page, String since_id) {
		StatusTimelineGson result = null;
		String request_url = AppContext.mention_timeline + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&page=" + page + "&since_id=" + since_id;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				result = gson.fromJson(response, StatusTimelineGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 删除一条评论
	 * @param cid
	 * @return
	 */
	public CommentEntry destroyComment(String cid) {
		CommentEntry entry = null;
		String request_url = AppContext.destroy_comment;
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
		paramsMap.put("cid", cid);
		try {
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				entry = gson.fromJson(response, CommentEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entry;
	}

	/**
	 * 获取用户发表的微博
	 * @param uid
	 * @param count
	 * @param page
	 * @return
	 */
	public StatusTimelineGson getUserTimeline(String uid, int count, int page, String since_id) {
		StatusTimelineGson result = null;
		String request_url = AppContext.get_user_timeline + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&page=" + page + "&uid=" + uid + "&since_id=" + since_id;;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				result = gson.fromJson(response, StatusTimelineGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取用户关注列表
	 * @param uid
	 * @param count
	 * @param cursor 返回结果的游标，下一页用返回值里的next_cursor，上一页用previous_cursor，默认为0。
	 * @return
	 */
	public UserGson getFriends(String uid, int count, int cursor) {
		UserGson result = null;
		String request_url = AppContext.get_user_friends + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&cursor=" + cursor + "&uid=" + uid;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				result = gson.fromJson(response, UserGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取用户粉丝列表
	 * @param uid
	 * @param count
	 * @param cursor
	 * @return
	 */
	public UserGson getFollowers(String uid, int count, int cursor) {
		UserGson result = null;
		String request_url = AppContext.get_user_followers + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&cursor=" + cursor + "&uid=" + uid;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				result = gson.fromJson(response, UserGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 发表一条微博
	 * @param status
	 * @param lat
	 * @param lon
	 * @return
	 */
	public StatusTimelineEntry updateStatus(String status, double lat, double lon) {
		StatusTimelineEntry result = null;
		String request_url = AppContext.update_status;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("status", status);
			paramsMap.put("lat", String.valueOf(lat));
			paramsMap.put("long", String.valueOf(lon));
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				result = gson.fromJson(response, StatusTimelineEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 发表一条带图片的微博
	 * @param status
	 * @param lat
	 * @param lon
	 * @param file
	 * @return
	 */
	public StatusTimelineEntry uploadStatus(String status, double lat, double lon, File file) {
		StatusTimelineEntry result = null;
		String request_url = AppContext.upload_status;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("status", status);
			paramsMap.put("lat", String.valueOf(lat));
			paramsMap.put("long", String.valueOf(lon));
			Map<String, File> files = new HashMap<String, File>();
			files.put("pic", file);
			String response = HttpUtils.post(request_url, paramsMap, files);
			if (response != null) {
				result = gson.fromJson(response, StatusTimelineEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 *关注某用户
	 *下面二个参数可以二选一
	 * @param uid
	 * @param screen_name
	 */
	public UserEntry friendCreate(String uid, String screen_name) {
		UserEntry result = null;
		String request_url = AppContext.friend_create;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("uid", uid);
			paramsMap.put("screen_name", screen_name);
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				result = gson.fromJson(response, UserEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 取消关注某用户
	 * @param uid
	 * @param screen_name
	 * @return
	 */
	public UserEntry friendDestroy(String uid, String screen_name) {
		UserEntry result = null;
		String request_url = AppContext.friend_create;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("uid", uid);
			paramsMap.put("screen_name", screen_name);
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				result = gson.fromJson(response, UserEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 转发一条微博
	 * @param status
	 * @param id
	 * @param is_comment 是否在转发的同时发表评论，0：否、1：评论给当前微博、2：评论给原微博、3：都评论，默认为0 。
	 * @return
	 */
	public StatusTimelineEntry repost(String status, String id, int is_comment) {
		StatusTimelineEntry result = null;
		String request_url = AppContext.repost;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("status", status);
			paramsMap.put("id", id);
			paramsMap.put("is_comment", String.valueOf(is_comment));
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				result = gson.fromJson(response, StatusTimelineEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 删除一条微博
	 * @param id
	 * @return
	 */
	public StatusTimelineEntry deleteStatus(String id) {
		StatusTimelineEntry result = null;
		String request_url = AppContext.destroy_status;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("id", id);
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				result = gson.fromJson(response, StatusTimelineEntry.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取当前登录用户的收藏列表
	 * @param count
	 * @param page
	 * @return
	 */
	public List<StatusTimelineEntry> getFavorites(int count, int page) {
		List<StatusTimelineEntry> result = null;
		String request_url = AppContext.get_favorites + "?access_token=" + AppContext.getInstance().getAccess_token()
				+ "&count=" + count + "&page=" + page;
		try {
			String response = HttpUtils.doGet(context, request_url);
			if (response != null) {
				System.out.println(response);
				FavoritesGson gsonResult = gson.fromJson(response, FavoritesGson.class);
				result = gsonResult.getFavorites();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 添加一条微博到收藏里
	 * @param id
	 * @return
	 */
	public FavoriteEntryGson createFavorite(String id) {
		FavoriteEntryGson result = null;
		String request_url = AppContext.create_favorite;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("id", id);
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				result = gson.fromJson(response, FavoriteEntryGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 取消收藏一条微博
	 * @param id
	 * @return
	 */
	public FavoriteEntryGson destroyFavorite(String id) {
		FavoriteEntryGson result = null;
		String request_url = AppContext.destroy_favorite;
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("access_token", AppContext.getInstance().getAccess_token());
			paramsMap.put("id", id);
			String response = HttpUtils.doPost(context, request_url, paramsMap);
			if (response != null) {
				result = gson.fromJson(response, FavoriteEntryGson.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
